package test.com.sky.designPattern.observerPattern.listener;

import com.sky.JULLog.MyLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author : wushikai
 * <p>
 * date : 2022-05-27
 *
 * 用户注册后，监听器执行保存数据入库和发送邮件的功能  数据源User类
 */
public class User {


    private static Logger log = MyLogger.getLogger(User.class);

    private String userId;

    String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * mark 核心组件, 事件监听 监控
     * */
    private List<MyEventListener> EventListenerMonitor = new ArrayList<>();

    User(String userId) {
        this.userId = userId;
    }


    void saveUser() {
        log.info("保存用户 ["+this.getUserId()+"] 到数据库...");
    }


    void setListener(MyEventListener listener) {
        EventListenerMonitor.add(listener);
    }

    void register() {
        log.info( "用户 ["+ this.userId +"] 已注册");
        for (MyEventListener listener : EventListenerMonitor) {
            UserEvent event = new UserEvent(this);
            listener.handleRegSuccess(event);                      //调用监听器的方法;
        }
    }
}
